<?php

class SettingController extends FController
{
   public function __construct($id,$module=null) {
       parent::__construct($id,$module);
        $this->menu=array(
            array('label'=>t('Manage Settings','setting'), 'url'=>array('/setting/manager'),'linkOptions'=>array('class'=>'btn')),
            array('label'=>t('General Settings','setting'), 'url'=>array('/setting/general'),'linkOptions'=>array('class'=>'btn')),
            array('label'=>t('System Settings','setting'), 'url'=>array('/setting/system'),'linkOptions'=>array('class'=>'btn')),
            array('label'=>t('Add Setting','setting'), 'url'=>array('/setting/add'),'linkOptions'=>array('class'=>'btn')),
        );
    }
	public function actionSystem()
	{

        $model = new systemSettingForm();
        settings()->deleteCache();
        $model->emailAdmin = settings()->get('system','emailAdmin');
        $model->emailFromReply = settings()->get('system','emailFromReply');
        $model->emailFromNoReply = settings()->get('system','emailFromNoReply');
        $model->emailServer = settings()->get('system','emailServer');
        $model->emailUser = settings()->get('system','emailUser');
        $model->emailPassword = settings()->get('system','emailPassword');
        $model->emailPort = settings()->get('system','emailPort');
        $model->emailSSL = settings()->get('system','emailSSL');

        $model->yb_username = settings()->get('system','yb_username');
        $model->yb_password = settings()->get('system','yb_password');
        $model->yb_service = settings()->get('system','yb_service');
        $model->yb_source = settings()->get('system','yb_source');
        $model->yb_authenticationURL = settings()->get('system','yb_authenticationURL');
        $model->yb_devkey = settings()->get('system','yb_devkey');
        $model->yb_category = settings()->get('system','yb_category');
        $model->yb_handler_url = settings()->get('system','yb_handler_url');

        $model->time_zone = settings()->get('system','time_zone');
        $model->time_format = settings()->get('system','time_format');

        $model->google_analytics = settings()->get('system','google_analytics');
        $model->gzip = settings()->get('system','gzip');

        $model->keep_file_name_upload = settings()->get('system','keep_file_name_upload');

        $model->cache_time = settings()->get('system','cache_time');

        $this->performAjaxValidation($model,'setting-form');

        if(isset($_POST['systemSettingForm'])) {
            settings()->deleteCache();
            $model->attributes=$_POST['systemSettingForm'];
            if($model->validate()){
                foreach($model->attributes as $key=>$value){
                    settings()->set('system', $key, $value);
                }
                user()->setFlash('success',t('<span class="label label-success">Success</span> System Settings Updated Successfully!','setting'));

            }
        }

		$this->render('system',array('model'=>$model));
	}
    public function actionGeneral()
	{
        $model = new generalSettingForm();
        settings()->deleteCache();
        $model->site_name = settings()->get('general','site_name');
        $model->site_offline = settings()->get('general','site_offline');
        $model->site_offline_message = settings()->get('general','site_offline_message');
        $model->site_offline_custom_message = settings()->get('general','site_offline_custom_message');
        $model->site_default_editor = settings()->get('general','site_default_editor');
        $model->site_page_size = settings()->get('general','site_page_size');

        $model->site_mainTitle = settings()->get('general','site_mainTitle');
        $model->site_mainKeyWord = settings()->get('general','site_mainKeyWord');
        $model->site_mainDescription = settings()->get('general','site_mainDescription');
        $model->site_generator = settings()->get('general','site_generator');
        $model->site_robots = str_replace(',',' |',settings()->get('general','site_robots'));

        $model->logo = settings()->get('general','logo');
        $model->no_image_available = settings()->get('general','no_image_available');
        $model->icon = settings()->get('general','icon');

        $model->image_avatar_small_width = settings()->get('general','image_avatar_small_width');
        $model->image_avatar_small_height = settings()->get('general','image_avatar_small_height');
        $model->image_avatar_medium_width = settings()->get('general','image_avatar_medium_width');
        $model->image_avatar_medium_height = settings()->get('general','image_avatar_medium_height');
        $model->image_avatar_large_width = settings()->get('general','image_avatar_large_width');
        $model->image_avatar_large_height = settings()->get('general','image_avatar_large_height');

        $model->image_thumbnail_small_width = settings()->get('general','image_thumbnail_small_width');
        $model->image_thumbnail_small_height = settings()->get('general','image_thumbnail_small_height');
        $model->image_thumbnail_medium_width = settings()->get('general','image_thumbnail_medium_width');
        $model->image_thumbnail_medium_height = settings()->get('general','image_thumbnail_medium_height');
        $model->image_thumbnail_large_width = settings()->get('general','image_thumbnail_large_width');
        $model->image_thumbnail_large_height = settings()->get('general','image_thumbnail_large_height');

        $model->image_crop = settings()->get('general','image_crop');
        $model->image_watermark = settings()->get('general','image_watermark');
        $model->image_watermark_image = settings()->get('general','image_watermark_image');
        $model->image_watermark_text = settings()->get('general','image_watermark_text');

        $this->performAjaxValidation($model,'setting-form');

        if(isset($_POST['generalSettingForm'])) {
            settings()->deleteCache();
            $model->attributes=$_POST['generalSettingForm'];
            if($model->validate()){
                foreach($model->attributes as $key=>$value){
                    settings()->set('general', $key, $value);
                }
                user()->setFlash('success',t('<span class="label label-success">Success</span> General Settings Updated Successfully!','setting'));

            }
        }

		$this->render('general',array('model'=>$model));
	}
    public function actionAdd(){
        $model = new Settings();
        $request = app()->request;
        $this->performAjaxValidation($model,'setting-add');
        if(isset($_POST['Settings'])) {
            $setting  = $request->getPost('Settings');
            settings()->set($setting['category'],$setting['key'],$setting['value']);
            user()->setFlash('success',t('<span class="label label-success">Success</span>  Setting has been created successfully','setting'));
        }
        $this->render('AddSystem',array('model'=>$model));

    }
    protected function performAjaxValidation($model,$form)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']===$form)
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}